Imports

Imports are the mechanism which make it possible to bring functionality from other Python libraries into your own code.


In [1]:
from math import pi, sin

The above expression brings in pi and sin from the math library.


In [2]:
pi # The constant pi


Out[2]:
3.141592653589793

In [3]:
sin(pi*1/2.) # The function sin


Out[3]:
1.0

You can also just import a library and not any particular function.


In [4]:
import math
math.e


Out[4]:
2.718281828459045

Finally in some cases you want to import a library, but the name of the library makes it cumbersome to use in your code. You can specify the name which you want to use for the library as follows.


In [5]:
import math as m
m.e


Out[5]:
2.718281828459045